﻿using System;
using System.Text;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;
using Skyworth.CRM.Service.Entity;

namespace Skyworth.CRM.Web.BasicData
{
    public partial class EditCustomer : BasePage
    {
        private ICustomerBLL custBll = (ICustomerBLL)IocContainer.Instance.Resolve<ICustomerBLL>();
        private ICustContactBLL contactBll = (ICustContactBLL)IocContainer.Instance.Resolve<ICustContactBLL>();
        private ICustDreeBLL dreeBll = (ICustDreeBLL)IocContainer.Instance.Resolve<ICustDreeBLL>();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["Type"]) && Request.QueryString["Type"].ToLower() == "update")
                {
                    hidCustId.Value = Request.QueryString["CustId"];
                    DataTable dt = custBll.Query(string.Format(" and FItemId={0}", Int32.Parse(hidCustId.Value)));                    
                    if (dt.Rows.Count > 0)
                    {
                        DataRow dr = dt.Rows[0];
                        // 加载客户
                        this.txtCustCode.Text = dr["FNumber"].ToString();
                        this.txtCustName.Text = dr["FName"].ToString();// custModel.FName;
                        base.BinDropDownList<DropDownList>(this.ddlstCustLevel,dr["FJB"].ToString(),true);
                        this.hidEmpId.Value = dr["FSalesId"].ToString();// custModel.FSalesId.ToString();
                        this.txtEmployee.Text = dr["EmpName"].ToString();
                        this.txtQuHao.Text = dr["FQH"].ToString();// custModel.FQH;
                        this.txtDianhua.Text = dr["FTel"].ToString();// custModel.FTel;
                        this.txtShouji.Text = dr["FMobil"].ToString();// custModel.FMobil;
                        this.txtDescription.Text = dr["FDree"].ToString();// custModel.FDree;
                        this.txtCustBrand.Text = dr["FCustBrand"].ToString();
                        this.txtCustMarket.Text = dr["FCustMarket"].ToString();
                        this.txtProducts.Text = dr["FProducts"].ToString();
                        this.txtChannels.Text = dr["FChannels"].ToString();
                        this.txtWebAddress.Text = dr["FWebAddress"].ToString();
                        // 加载联系人列表
                        StringBuilder sbContact = new StringBuilder();
                        DataTable dtContact = contactBll.Query(string.Format(" FCustId={0}", hidCustId.Value));
                        if (dtContact != null && dtContact.Rows.Count > 0)
                        {
                            foreach (DataRow dataRow in dtContact.Rows)
                            {
                                sbContact.AppendFormat("<tr><td>{0}</td><td>{1}</td>", dataRow["FName"].ToString(),dataRow["FEmail"].ToString());
                                sbContact.AppendFormat("<td>{0}</td><td>{1}</td>", dataRow["FTel"].ToString(), dataRow["FMobile"].ToString());
                                sbContact.AppendFormat("<td>{0}</td><td>{1}</td>", dataRow["FIsD"].ToString() == "1" ? "true" : "false", dataRow["FDree"].ToString());
                                sbContact.AppendFormat("<td>{0}</td><td>{1}</td>", dataRow["FHobby"].ToString(), dataRow["FNote"].ToString());
                                sbContact.Append("<td><a href='javascript:void(0)' onclick='remove(this)'>删除</a></td></tr>");
                            }
                            litContactList.Text = sbContact.ToString();
                        }                        
                        // 加载收货地址列表
                        StringBuilder sbAdd = new StringBuilder();
                        DataTable dtAdd = dreeBll.Query(string.Format(" FCustId={0}", hidCustId.Value));
                        if (dtAdd != null && dtAdd.Rows.Count > 0)
                        {
                            foreach (DataRow dr2 in dtAdd.Rows)
                            {
                                sbAdd.AppendFormat("<tr><td>{0}</td><td>{1}</td>", dr2["FName"].ToString(), dr2["FTel"].ToString());
                                sbAdd.AppendFormat("<td>{0}</td><td>{1}</td>", dr2["FMobile"].ToString(), dr2["FNumber"].ToString());
                                sbAdd.AppendFormat("<td>{0}</td>", dr2["FDreeName"].ToString());
                                sbAdd.Append("<td><a href='javascript:void(0)' onclick='remove(this)'>删除</a></td></tr>");
                            }
                            litAddressList.Text = sbAdd.ToString();
                        }
                    }
                }
            }
        }
    }
}